package com.fuyao.cloud.ureport.datasource;

import cn.hutool.core.util.StrUtil;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import com.fuyao.cloud.ureport.config.UreportDataSourceConfig;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 资产空间内置数据源
 *
 * @author lyhil
 * @date 2021.12.20
 */
@Component
public class InnerDataSourceAsset implements BuildinDatasource {

	@Autowired
	private UreportDataSourceConfig ureportDataSourceConfig;

	@Override
	public String name() {
		return "asset_datasource";
	}

	@SneakyThrows
	@Override
	public Connection getConnection() {
		Class.forName(UreportDataSourceConfig.DRIVER);
		String url = StrUtil.builder().append("jdbc:mysql://")
				.append(ureportDataSourceConfig.getIp())
				.append(":")
				.append(ureportDataSourceConfig.getPort())
				.append("/cloud_asset?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true").
				toString();
		return DriverManager.getConnection(url, ureportDataSourceConfig.getUser(), ureportDataSourceConfig.getPassword());
	}
}
